package com.lennon.oauth.store.impl;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import com.lennon.oauth.domain.Code;
import com.lennon.oauth.store.AbstrctGeneratorUUID;
import com.lennon.oauth.store.ICodeStore;

public class CodeStore extends AbstrctGeneratorUUID implements ICodeStore{

	private Map<String/**code*/, Code/**clientInfo*/> codeInfoHolder = new ConcurrentHashMap<String, Code>();
	
	@Override
	public String createUUIDCode(String clientId) {
		String code = generateCode();
		codeInfoHolder.put(code, new Code(code, clientId, true));
		return code;
	}
	
	@Override
	public boolean isRightCode(String code, String clientId) {
		Code codeInfo = codeInfoHolder.get(code);
		if(codeInfo != null && codeInfo.isState()) {
			if(codeInfo.getCode() != null && codeInfo.getCode().equals(code)
					&& codeInfo.getClientId() != null && codeInfo.getClientId().equals(clientId)) {
				return true;
			}
		}
		return false;
	}
	
}
